﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace BITUI.Audio
{
    class BackGroundMixer : AudioSystem.IMixer
    {
        List<IAudioData> layers = new List<IAudioData>();
        public void TryLoadAndPlay(string filename, bool loop, float fadintime)
        {
            
            layers.Clear();
            
            var ext = System.IO.Path.GetExtension(filename).ToLower();
            {
                AudioData_Ogg layer = new AudioData_Ogg();
                foreach(var en in layer.ExtNames)
                {
                    if(en.ToLower()==ext)
                    {
                        layer.Load(filename);
                        layers.Add(layer);
                        return;
                    }
                }

            }

            {
                AudioData_Mp3 layer = new AudioData_Mp3();
                foreach (var en in layer.ExtNames)
                {
                    if (en.ToLower() == ext)
                    {
                        layer.Load(filename);
                        layers.Add(layer);
                        return;
                    }
                }

            }
        }

        public unsafe void MixFloat_44100_2(float* stream, int streamlen)
        {
            foreach (var data in layers)
            {
                float[] buf = new float[streamlen];
                var muslen = AudioDataConverter.ReadFloat_44100_2(data, buf, 0, streamlen);
                //var muslen = l.Read_44100_2(buf, 0, streamlen);
                AudioSystem.MixFloatAudio(stream, 0, streamlen,
                    buf, 0, muslen);
            }
        }
    }


}
